package com.example.dllo.exe.giftspeak;

import android.content.Intent;
import android.graphics.Color;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.example.dllo.exe.R;
import com.example.dllo.exe.giftspeak.jingxuan.JingXuanFragment;
import com.example.dllo.exe.giftspeak.songnvpiao.SongNvPiaoTopFragment;
import com.example.dllo.exe.main.BaseFragment;
import com.example.dllo.exe.fengzhuang.DBValue;

import java.util.ArrayList;

/**
 * code is far away from bug with the animal protecting
 * <p/>
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃永无BUG！  凯哥 祝你一路顺风
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 */
public class GiftSpeakFragment extends BaseFragment implements View.OnClickListener {
    private ArrayList<String> list;
    private ArrayList<Fragment> fragments;
    private ImageView imageViewgiftspeakchouti;
    private TabLayout tabLayoutGiftSpeak;
    private ViewPager viewPagerGiftSpeak;

    private PopupWindow pw;
    private ImageView imageViewsousuo;
    private GridViewPopAdapter gridViewPopAdapter;

    @Override
    protected int setLayout() {
        return R.layout.giftspeakfragmentlayout;
    }

    @Override
    protected void initView() {
        tabLayoutGiftSpeak = bindView(R.id.tab_layout_giftspeak);
        viewPagerGiftSpeak = bindView(R.id.view_pager_giftspeak);
        imageViewgiftspeakchouti = bindView(R.id.image_giftspeakchouti);
        imageViewsousuo=bindView(R.id.image_sousuo);
    }

    @Override
    protected void initData() {
        GiftSpeakAdapter giftSpeakAdapter = new GiftSpeakAdapter(getChildFragmentManager(), getContext());

        list = new ArrayList<String>();
        list.add("精选");
        list.add("送女票");
        list.add("海淘");
        list.add("创意生活");
        list.add("送基友");
        list.add("送爸妈");
        list.add("送同事");
        list.add("送宝贝");
        list.add("设计感");
        list.add("文艺风");
        list.add("奇葩搞怪");
        list.add("科技范");
        list.add("萌萌哒");
        String[] urls = {DBValue.GIFTSPEAK_SONGNVPIAO, DBValue.GIFTSPEAK_HAITAO, DBValue.GIFTSPEAK_CHUANGYISHENGHUO, DBValue.GIFTSPEAK_CHUANGYISHENGHUO, DBValue.GIFTSPEAK_SONGJIYOU, DBValue.GIFTSPEAK_SONGBAMA, DBValue.GIFTSPEAK_SONGTONGSHI, DBValue.GIFTSPEAK_SONGBAOBEI, DBValue.GIFTSPEAK_SHEJIGAN, DBValue.GIFTSPEAK_WENYIFENG, DBValue.GIFTSPEAK_QIPAGAOGUAI, DBValue.GIFTSPEAK_KEJIFAN, DBValue.GIFTSPEAK_MENGMENGDA};
        fragments = new ArrayList<Fragment>();
        fragments.add(new JingXuanFragment());

        for (int i = 0; i < 12; i++) {

            fragments.add(SongNvPiaoTopFragment.getInstance(urls[i]));
        }

        giftSpeakAdapter.setFragments(fragments);
        giftSpeakAdapter.setList(list);
        viewPagerGiftSpeak.setAdapter(giftSpeakAdapter);
        tabLayoutGiftSpeak.setupWithViewPager(viewPagerGiftSpeak);
        tabLayoutGiftSpeak.setSelectedTabIndicatorColor(Color.RED);
        tabLayoutGiftSpeak.setTabTextColors(Color.BLACK, Color.RED);
        imageViewgiftspeakchouti.setOnClickListener(this);
        imageViewsousuo.setOnClickListener(this);
    }







    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case  R.id.image_sousuo:
                Intent intent=new Intent(getContext(),GiftSpeakSouSuoAty.class);
                startActivity(intent);

                break;
            case  R.id.image_giftspeakchouti:

                pw = new PopupWindow(getContext());

                if(pw!=null&&pw.isShowing()){
                    pw.dismiss();
                    return;
                }else {
                    initmPopupWindowView();
                    pw.showAsDropDown(view,0,0);

                }
                break;
 default:
                break;
        }



    }

    private void initmPopupWindowView() {
        //获取自定义布局的pop.xml的视图
        final View popView = LayoutInflater.from(getContext()).inflate(R.layout.pop_window, null);
        //设置popupwindow大小
          pw=new PopupWindow(popView,800,1000);
        //pw.setTouchable(false);
        //点击其他位置消失
        pw.setOutsideTouchable(true);

        final GridView gridViewpop= (GridView) popView.findViewById(R.id.grid_view_pop);
        gridViewPopAdapter = new GridViewPopAdapter(getContext());

        gridViewpop.setAdapter(gridViewPopAdapter);
        //GridView的点击事件用OnItemClickListener
        gridViewpop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
               Log.d("GiftSpeakFragment", "i"+i);
                //让tablayot 滚动到第几个
                tabLayoutGiftSpeak.setScrollPosition(i,0,true);
                tabLayoutGiftSpeak.getTabAt(i).select();
                gridViewpop.setCacheColorHint(Color.RED);
                //点击确定后 消失

                pw.dismiss();
            }
        });
//        pw.setAnimationStyle(R.anim.activity_out);
        //点击响应其他部位部位消失
        popView.setOnTouchListener(new View.OnTouchListener() {
           @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (pw!=null&&pw.isShowing()){
                    pw.dismiss();
                    pw=null;
                }

                return false;
            }
        });


    }
}

    

